package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Queue;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import model.map.MapPoint;
import model.map.ViewMap;

/**
 * This class is where the actual game map is rendered and what the user sees
 * when they interact with the avatar. It is held in the GamePanel.
 * 
 * @author Stephanie
 *
 */
public class MapPanel extends JPanel {
	private BufferedImage image;
	private BufferedImage imgBuffer;
	private BufferedImage four0four;
	private Graphics2D imgBuffer_g2;
	private ViewMap map;
	private ImageCache imageCache;
	public MapPanel() {
		setSize(600,420);
		setLocation(0,0);
		setVisible(true);
		setLayout(null);
		imageCache = new ImageCache();
		four0four = imageCache.getNotFoundImage();
	}
	
		
	public void setMap(ViewMap map){
		if (map == null){
			System.out.println("MAP IS NULL");
		}
		this.map = map;
	}
	public void refresh(Graphics g){
		//gatherInfomation();
		if(imgBuffer == null)
			System.out.println("BufferImage == null");
		//image = imgBuffer;
	}
	private void gatherInfomation(){
		Dimension d = getSize();
	    int width = d.width; 
	    int height = d.height;
	    imgBuffer  = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR );
	    if(imgBuffer == null)
	    	System.out.println("BufferImg is null here");
	    imgBuffer_g2 = imgBuffer.createGraphics();
	    if(map != null){
			MapPoint center = map.getRenderLocation();
			for(int y = -22;y<=22;y++){
				for(int x = -14;x<=14;x++){
					if((x+y)%2 == 0){
						Queue<String> q = map.getRenderablesFromPoint(MapPoint.addPoints(center, new MapPoint(y, x)));
						drawQueue(q,x,y);
					}
				}
			}
	    }
	}
	private void drawQueue(Queue<String> q, int x, int y){
		
		if(q == null){
			drawImageAt(imageCache.getImage("queue"),x,y);
		}
		else{
			Iterator<String> iter = q.iterator();
			BufferedImage img;
			while(iter.hasNext()){
				String s = iter.next();
				System.out.println(s);
				img = imageCache.getImage(s);
			    drawImageAt(img,x,y);
			}
		}
	}
	private void drawImageAt(BufferedImage img, int x, int y){
		int x_mult = 22;
	    int y_mult = 10;
	    int x_offset_pixel = 0;
	    int y_offset_pixel = 0;
	    int x_offset_coord = 13;
	    int y_offset_coord = 16;
	    /*int x_mult = 22;
	    int y_mult = 10;
	    int x_offset_pixel = 4;
	    int y_offset_pixel = 0;
	    int x_offset_coord = 12;
	    int y_offset_coord = 18;*/
	    //System.out.println("x: "+x+" y: "+y);
	    //System.out.println("xp: "+(x+x_offset_coord)*x_mult+x_offset_pixel+" yp: "+(y+y_offset_coord)*y_mult+y_offset_pixel);
	    imgBuffer_g2.drawImage(img, (x+x_offset_coord)*x_mult+x_offset_pixel, (y+y_offset_coord)*y_mult+y_offset_pixel, null);
	}
	protected void paintComponent(Graphics g)
	{
		g.setColor(Color.BLUE);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.setColor(Color.BLACK);
		g.drawString("Map",50,50);
		
	}
	public void paint(Graphics g) {
	    Graphics2D g2 = (Graphics2D) g;
	    g.setColor(Color.BLUE);
		g.fillRect(0, 0, getWidth(), getHeight());
	    bufferImage();
	    if(imgBuffer != null){
	    	g2.drawImage(imgBuffer,0,0,null);
	    }
	}
	
	private void bufferImage(){	
		Dimension d = getSize();
		int width = d.width; 
		int height = d.height;
		imgBuffer  = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR );
		imgBuffer_g2 = imgBuffer.createGraphics();
		BufferedImage image1 = imageCache.getImage("exampleTile");
		BufferedImage image2 = imageCache.getImage("NPCs\\medusa");
		BufferedImage image3 = imageCache.getImage("Avatars\\sneak1");
		int x = 22;
		int y = 10;
		int offsetx = 4;
		int offsety = 0;
		/*for(int i = 5;i<15;i++){
			for(int j = 5;j<20;j++){
				if((i+j)%2 == 0){
					imgBuffer_g2.drawImage(image1, x*i+offsetx,y*j+offsety, null);
					drawImageAt(image1, x,y, imgBuffer_g2);
				}
			}
		}*/
		for(int i = -14;i<=14;i++){
			for(int j = -22;j<=22;j++){
				if((i+j)%2 == 0)
					drawImageAt(image1, i,j);
			}
		}
		gatherInfomation();
	}
	/*
	private void drawImageAt(BufferedImage img, int x, int y,Graphics2D g2){
		int x_mult = 22;
	    int y_mult = 10;
	    int x_offset_pixel = 0;
	    int y_offset_pixel = 0;
	    int x_offset_coord = 13;
	    int y_offset_coord = 16;
	    //System.out.println("x: "+x+" y: "+y);
	    //System.out.println("xp: "+(x+x_offset_coord)*x_mult+x_offset_pixel+" yp: "+(y+y_offset_coord)*y_mult+y_offset_pixel);
	    	g2.drawImage(img, (x+x_offset_coord)*x_mult+x_offset_pixel, (y+y_offset_coord)*y_mult+y_offset_pixel, null);
	}*/
}
